package com.flute.icrawler.framework.framework;

import java.io.Serializable;

import com.flute.icrawler.app.processor.AbstractProcessor;
import com.flute.icrawler.framework.framework.container.AdditionalUrlInformation;
import com.flute.icrawler.framework.framework.container.IAdditional;
import com.flute.icrawler.framework.framework.container.NumberStringValue;
import com.flute.icrawler.framework.framework.container.AdditionalUrlInformation.Additionals;
import com.flute.icrawler.framework.processor.result.CrawlUrlResult;
import com.flute.icrawler.framework.processor.result.IProcessResult;
import com.flute.icrawler.framework.processor.result.ResultParameter;

/**
 * 爬虫url定义
 * 
 * @author Administrator
 * 
 */
public class CrawlUrl implements Serializable, IAdditional {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1983745420425000565L;
	/**
	 * 已经处理过的步骤
	 */
	private ProcessorClassList processedList;
	/**
	 * 正在处理的步骤
	 */
	private ProcessorClassList processingList;
	/**
	 * 处理结果
	 */
	private CrawlUrlResult result;
	/**
	 * 网页url地址
	 */
	private String url;

	private AdditionalUrlInformation info;
	/**
	 * 从属于的爬行任务
	 */
	private CrawlJob job;

	public CrawlUrl(CrawlJob job, String url) {
		this.processedList = new ProcessorClassList();
		this.processingList = new ProcessorClassList();
		this.result = new CrawlUrlResult();
		this.job = job;
		this.url = url;
		this.info = new AdditionalUrlInformation();
	}

	public void initProcessorList() {
		this.processedList = new ProcessorClassList();
		this.processingList = new ProcessorClassList();
	}

	/**
	 * 记录自己已经被哪个IProcessor处理过了
	 * 
	 * @param processorClass
	 */
	public synchronized void processed(AbstractProcessor processorClass) {
		this.processingList.remove(processorClass);
		this.processedList.addProcessor(processorClass);
		// computeNextProcessors();
	}

	/**
	 * 记录自己正在被哪个IProcessor处理
	 * 
	 * @param processorClass
	 */
	public synchronized void processing(AbstractProcessor processorClass) {
		this.processingList.addProcessor(processorClass);
		// computeNextProcessors();
	}

	

	

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void registerProcessorResult(IProcessResult pr) {
		if (!result.contain(pr)) {
			result.addProcessorResult(pr);
		}
	}
	

	public void registerResultParameter(ResultParameter parameter) {
		result.addResultParameter(parameter);
	}
	


	public ResultParameter getResultParameter(String name) {
		return result.getResultParameter(name);
	}

	public boolean needInterrupt() {
		return result.isCanInterrupt();
	}

	public CrawlUrlResult getResult() {
		return result;
	}

	public int getDeep() {
		return ((NumberStringValue) info.getValue(Additionals.Deep))
				.getNumber().intValue();
	}

	public int getRetryCount() {
		return ((NumberStringValue) info.getValue(Additionals.RetryCount))
				.getNumber().intValue();
	}

	public int getPosition() {
		return ((NumberStringValue) info.getValue(Additionals.Position))
				.getNumber().intValue();
	}

	public int getResourceID() {
		return ((NumberStringValue) info.getValue(Additionals.ResourceID))
				.getNumber().intValue();
	}

	public int getUpdateCount() {
		return ((NumberStringValue) info.getValue(Additionals.UpdateCount))
				.getNumber().intValue();
	}

	@Override
	public AdditionalUrlInformation getAdditionalUrlInformation() {
		return info;
	}

	public void setAdditionalUrlInformation(AdditionalUrlInformation info) {
		this.info = info;
	}

	public ProcessorClassList getProcessedList() {
		return processedList;
	}

	public ProcessorClassList getProcessingList() {
		return processingList;
	}

	public String toString() {
		return url + " features:" + info.toString();
	}

	public CrawlJob getJob() {
		return job;
	}

	public void setJob(CrawlJob job) {
		this.job = job;
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof CrawlUrl) {
			CrawlUrl url = (CrawlUrl) obj;

			if (url.getUrl().equalsIgnoreCase(this.getUrl())
					&& url.getUpdateCount() == this.getUpdateCount()) {
				return true;
			}

		}
		return super.equals(obj);
	}

	public void resetProcessingInfos() {
		this.job = null;
		this.processedList = new ProcessorClassList();
		this.processingList = new ProcessorClassList();
		this.result = new CrawlUrlResult();
	}
	

}